package com.hkbigdata.sybx.controller;

import com.hkbigdata.sybx.bean.PlayCount;
import com.hkbigdata.sybx.bean.Video;
import com.hkbigdata.sybx.service.PlayCountService;
import com.hkbigdata.sybx.service.VideoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liuanbo
 * @creat 2025-06-14-20:20
 * @see 2194550857@qq.com
 */
@RestController
@RequestMapping("/api/videos")
@Slf4j
public class VideoController {

    private final VideoService videoService;
    private final PlayCountService playCountService;

    // 使用构造函数注入
    @Autowired
    public VideoController(VideoService videoService, PlayCountService playCountService) {
        this.videoService = videoService;
        this.playCountService = playCountService;
    }

    @GetMapping("/list")
    public ResponseEntity<?> listVideos() {
        log.info("收到获取视频列表请求");
        try {
            List<Video> videos = videoService.listVideos();
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("data", videos);
            log.info("成功返回视频列表，共 {} 条", videos.size());
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("获取视频列表失败", e);
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", "获取视频列表失败");
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }

    @GetMapping("/playcountsselect")
    public ResponseEntity<?> getPlayCounts() {
        log.info("收到获取所有播放次数请求");
        try {
            List<PlayCount> playCounts = playCountService.getAllPlayCounts();
            log.info("成功获取播放次数列表，共 {} 条: {}", playCounts.size(), playCounts);
            return ResponseEntity.ok(new Object() {
                public boolean success = true;
                public List<PlayCount> data = playCounts;
            });
        } catch (Exception e) {
            log.error("获取播放次数列表失败", e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new Object() {
                public boolean success = false;
                public String message = "获取播放次数列表失败: " + e.getMessage();
            });
        }
    }

    @PostMapping("/playcountsincrement") // 使用 POST 方法来递增播放次数
    public ResponseEntity<?> incrementVideoPlayCount(@RequestBody PlayCount requestBody) {
        log.info("收到递增播放次数请求，请求体: {}", requestBody); // 记录完整的请求体
        if (requestBody == null || requestBody.getTitle() == null || requestBody.getTitle().isEmpty()) {
            log.warn("递增播放次数请求体无效或缺少视频标题。");
            return ResponseEntity.badRequest().body(new Object() {
                public boolean success = false;
                public String message = "视频标题不能为空。";
            });
        }
        try {
            PlayCount updatedPlayCount = playCountService.incrementPlayCount(requestBody.getTitle());
            String newCount = (updatedPlayCount != null && updatedPlayCount.getPlayCount() != null)
                    ? updatedPlayCount.getPlayCount()
                    : "0"; // 确保返回非null值

            log.info("视频 {} 播放次数已成功递增，新次数: {}", requestBody.getTitle(), newCount);
            return ResponseEntity.ok(new Object() {
                public boolean success = true;
                public String message = "播放次数已成功递增。";
                public String newPlayCount = newCount;
            });
        } catch (Exception e) {
            log.error("递增视频 {} 播放次数失败", requestBody.getTitle(), e); // 记录异常堆栈
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new Object() {
                public boolean success = false;
                public String message = "递增播放次数失败: " + e.getMessage();
            });
        }
    }
}